package com.garlicts.framework.crawler.scheduler;

import org.apache.commons.beanutils.BeanUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.garlicts.framework.crawler.common.IJobHandler;
import com.garlicts.framework.crawler.common.dto.Result;

/**
 * 远程QuartzJobBean，调度端没有具体的QuartzJobBean
 *
 */
@DisallowConcurrentExecution
public class RemoteQuartzJobBean extends QuartzJobBean {

	private static final Logger LOGGER = LoggerFactory.getLogger(RemoteQuartzJobBean.class);

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		JobKey jobKey = context.getTrigger().getJobKey();
		LOGGER.info("jobName:" + jobKey.getName() + ",group:" + jobKey.getGroup());
		
		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		ExecutorBean executorBean = new ExecutorBean();
		try {
			BeanUtils.populate(executorBean, jobDataMap);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		IJobHandler executor = CrawlerProxy.create(IJobHandler.class, jobKey, executorBean);
		Result result;
		try {
			result = executor.execute(executorBean.getJobContent());
			LOGGER.info("result:" + result);
		} catch (Exception e) {
			LOGGER.error("", e);
		}
	}

}